package com.web.servlet;

import com.web.entity.TestBiao;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 查看已申请考试列表
 * @author 12270
 */
@WebServlet("/sats")
public class SearchApplyTestServlet extends HttpServlet{
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        int tid = Integer.parseInt(req.getParameter("tid"));
        
        String driver_class = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://localhost:1433;databaseName=OnlineTestSystem";
        String username = "sa";
        String password = "sa";

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<TestBiao> list = new ArrayList<>();

        try {
            Class.forName(driver_class);
            con = DriverManager.getConnection(url, username, password);
            ps = con.prepareStatement("select * from test where tid=? order by testtime desc");
            ps.setInt(1, tid);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new TestBiao(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getInt(4), rs.getInt(5),rs.getInt(6)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                
                if (null != rs) rs.close();
                if (null!=ps) ps.close();
                if (null!=con) con.close();
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        HttpSession session = req.getSession();
        session.setAttribute("list", list);
        req.getRequestDispatcher("/SearchApplyTest.jsp").forward(req, resp);
    }
}
